package com.aizhixin.lab.project.course.service;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.project.course.domain.ReleaseProjectReplyDomain;
import com.aizhixin.lab.project.course.entity.ReleaseProjectReply;
import com.aizhixin.lab.project.course.repository.ReleaseProjectReplyRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Slf4j
@Service
@Transactional
public class ReleaseProjectReplyService {
    @Autowired
    private ReleaseProjectReplyRepository releaseProjectReplyRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;

    /**
     * 发布答辩
     * @param domain 注释见类中
     */
    public Map<String, Object> releaseProjectReply(ReleaseProjectReplyDomain domain){
        Map<String, Object> result = new HashMap<String, Object>();
        ReleaseProjectReply releaseProjectReply = new ReleaseProjectReply();
        BeanUtils.copyProperties(domain,releaseProjectReply);
        releaseProjectReply.setId(UUID.randomUUID().toString());
        releaseProjectReplyRepository.save(releaseProjectReply);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.MESSAGE, "发布成功");
        return result;
    }

    /**
     * 删除发布答辩
     * @param id 发布答辩id
     */
    public Map<String, Object> deleteReleaseProjectReply(String id){
        Map<String, Object> result = new HashMap<String, Object>();
        ReleaseProjectReply releaseProjectReply = releaseProjectReplyRepository.findByIdAndDeleteFlag(id, DataValidity.VALID.getState());
        releaseProjectReply.setDeleteFlag(DataValidity.INVALID.getState());
        releaseProjectReplyRepository.save(releaseProjectReply);
        result.put(ApiReturnConstants.SUCCESS, true);
        result.put(ApiReturnConstants.MESSAGE, "删除成功");
        return result;
    }

    /**
     * 查询该项目下发布的答辩列表
     * @param projectId 项目id
     */
    public List<ReleaseProjectReply> findReleaseProjectReply(String projectId){
        List<ReleaseProjectReply> releaseProjectReplyList = releaseProjectReplyRepository.findByProjectIdAndDeleteFlagOrderByCreateDateAsc(projectId, DataValidity.VALID.getState());
        return releaseProjectReplyList;
    }






}
